/**
 * 鸡西市采购网爬虫
 *  
 */
import {Base,Item} from '../base';
class jxszfcg extends Base{
    document:Document;
    constructor(){
        super();
        this.name = "鸡西市采购网";
        this.enabled = true;
        this.encode = 'utf-8';
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "ztb";
        this.collectionShowName = "招投标信息";
        this.keyField = "title";
    }
    getItems(pageIndex:number):Item[]{
        if(pageIndex==0){
            this.spider.getSync('http://www.hljcg.gov.cn/xwzs!index.action?dq=2302');
            try {
                this.spider.getSync('http://www.hljcg.gov.cn/welcome.jsp?dq=2303');
            } catch (error) {
                
            }
            this.spider.getSync('http://www.hljcg.gov.cn/xwzs!queryXwxxqx.action?lbbh=42303');
        }
        let num = pageIndex + 1;
        let postData={
            "xwzsPage.pageNo":num
        }
        let url = `http://www.hljcg.gov.cn/xwzs!queryXwxxqx.action`;
        let result :Item[]= [];
        let content = this.spider.postSync(url,postData);
        let $=this.cheerio.load(content);
        let that =this;
        $('.yahoo div.xxei').each(function(index,ele){
            let item =that.buildItem();
            item.title=$(ele).find('span').eq(0).find('a').text().trim();
            item.source=that.name;
            (<any>item)['公告时间']=$(ele).find('span').eq(1).text();
            (<any>item)['说明']=$(ele).find('span').eq(2).text();
            let  le=$(ele).find('span').eq(0).find('a').attr('onclick').length;
            let uri = 'http://www.hljcg.gov.cn' + $(ele).find('span').eq(0).find('a').attr('onclick').substring(26, le - 15);
            (<any>item).url = 'http://www.hljcg.gov.cn' + $(ele).find('span').eq(0).find('a').attr('onclick').substring(26, le - 15);
            result=that.getExtraItem(uri,item,result);
        });
        return result;
    }
    getExtraItem(url:string, item:Item, result:Item[]) {
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        (<any>item).content =$('#rightej').html();
        result.push(item);
        return result;
    }
    getPageCount() {
        return 20;
    }
}
module.exports=new jxszfcg();